#include <bits/stdc++.h>

using namespace std;

int n, m, pre[1505][1505], x[1505][1505], y[1505][1505];
char c[1505][1505];

void init(int k)
{
    memset(pre, 0, sizeof pre);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            pre[i][j] = (y[i][j] >= k) + pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1];
}

int sum(int x1, int y1, int x2, int y2)
{
    x1 = max(1, x1), y1 = max(1, y1);
    x2 = min(n, x2), y2 = min(n, y2);
    return pre[x2][y2] - pre[x1 - 1][y2] - pre[x2][y1 - 1] + pre[x1 - 1][y1 - 1];
}

bool check(int k)
{
    init(k);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (x[i][j] >= k && sum(1, 1, n, n) - sum(i - k + 1, j, i, j + k - 1) > 0)
                return true;
    int cnt1 = 0, cnt2 = 0;
    for (int i = 1; i <= n; i++) {
        bool flag = false;
        int last = 0;
        for (int j = 1; j <= n; j++) {
            if (x[i][j] >= k)
                flag = true, last = (last ? last : j);
            if (x[i][j] >= k && last && last + k <= j)
                return true;
        }
        cnt1 += flag;
        if (cnt1 >= 2)
            return true;
    }
    for (int j = 1; j <= n; j++) {
        bool flag = false;
        int last = 0;
        for (int i = 1; i <= n; i++) {
            if (y[i][j] >= k)
                flag = true, last = (last ? last : i);
            if (y[i][j] >= k && last && last + k <= i)
                return true;
        }
        cnt2 += flag;
        if (cnt2 >= 2)
            return true;
    }
    return false;
}

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> c[i] + 1;
    for (int i = 1; i <= n; i++)
        for (int j = n; j >= 1; j--)
            if (c[i][j] == '.')
                x[i][j] = x[i][j + 1] + 1;
    for (int j = 1; j <= n; j++)
        for (int i = n; i >= 1; i--)
            if (c[i][j] == '.')
                y[i][j] = y[i + 1][j] + 1;
    if (m == 2) {
        int l = 0, r = n;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (check(mid))
                l = mid + 1;
            else
                r = mid - 1;
        }
        cout << r;
    } else {
        int ans = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                ans = max(ans, max(x[i][j], y[i][j]));
        cout << ans;
    }
    return 0;
}